﻿using System;
using System.Reflection;
using System.Configuration;
namespace HYSoft.DALFactory
{
    /// <summary>
    /// Abstract Factory pattern to create the DAL。
    /// 如果在这里创建对象报错，请检查web.config里是否修改了<add key="DAL" value="Maticsoft.SQLServerDAL" />。
    /// </summary>
    public sealed class DataAccess
    {
        private static readonly string AssemblyPath = ConfigurationManager.AppSettings["DAL"];
        public DataAccess()
        { }

        #region CreateObject

        //不使用缓存
        private static object CreateObjectNoCache(string AssemblyPath, string classNamespace)
        {
            try
            {
                object objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace);
                return objType;
            }
            catch//(System.Exception ex)
            {
                //string str=ex.Message;// 记录错误日志
                return null;
            }

        }
        //使用缓存
        private static object CreateObject(string AssemblyPath, string classNamespace)
        {
            object objType = DataCache.GetCache(classNamespace);
            if (objType == null)
            {
                try
                {
                    objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace);
                    DataCache.SetCache(classNamespace, objType);// 写入缓存
                }
                catch//(System.Exception ex)
                {
                    //string str=ex.Message;// 记录错误日志
                }
            }
            return objType;
        }
        #endregion

        #region 泛型生成
        ///// <summary>
        ///// 创建数据层接口。
        ///// </summary>
        //public static t Create(string ClassName)
        //{

        //    string ClassNamespace = AssemblyPath +"."+ ClassName;
        //    object objType = CreateObject(AssemblyPath, ClassNamespace);
        //    return (t)objType;
        //}
        #endregion

        #region CreateSysManage
        public static HYSoft.IDAL.ISysManage CreateSysManage()
        {
            //方式1			
            //return (HYSoft.IDAL.ISysManage)Assembly.Load(AssemblyPath).CreateInstance(AssemblyPath+".SysManage");

            //方式2 			
            string classNamespace = AssemblyPath + ".SysManage";
            object objType = CreateObject(AssemblyPath, classNamespace);
            return (HYSoft.IDAL.ISysManage)objType;
        }
        #endregion



        /// <summary>
        /// 创建area数据层接口。
        /// </summary>
        public static HYSoft.IDAL.Iarea Createarea()
        {

            string ClassNamespace = AssemblyPath + ".area";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HYSoft.IDAL.Iarea)objType;
        }


        /// <summary>
        /// 创建base数据层接口。
        /// </summary>
        public static HYSoft.IDAL.Ibase Createbase()
        {

            string ClassNamespace = AssemblyPath + ".base";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HYSoft.IDAL.Ibase)objType;
        }


        /// <summary>
        /// 创建brd数据层接口。
        /// </summary>
        public static HYSoft.IDAL.Ibrd Createbrd()
        {

            string ClassNamespace = AssemblyPath + ".brd";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HYSoft.IDAL.Ibrd)objType;
        }


        /// <summary>
        /// 创建brd_log数据层接口。
        /// </summary>
        public static HYSoft.IDAL.Ibrd_log Createbrd_log()
        {

            string ClassNamespace = AssemblyPath + ".brd_log";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HYSoft.IDAL.Ibrd_log)objType;
        }


        /// <summary>
        /// 创建Bs数据层接口。
        /// </summary>
        public static HYSoft.IDAL.IBs CreateBs()
        {

            string ClassNamespace = AssemblyPath + ".Bs";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HYSoft.IDAL.IBs)objType;
        }


        /// <summary>
        /// 创建bsnode数据层接口。
        /// </summary>
        public static HYSoft.IDAL.Ibsnode Createbsnode()
        {

            string ClassNamespace = AssemblyPath + ".bsnode";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HYSoft.IDAL.Ibsnode)objType;
        }


        /// <summary>
        /// 创建command数据层接口。
        /// </summary>
        public static HYSoft.IDAL.Icommand Createcommand()
        {

            string ClassNamespace = AssemblyPath + ".command";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HYSoft.IDAL.Icommand)objType;
        }


        /// <summary>
        /// 创建gprsNode数据层接口。
        /// </summary>
        public static HYSoft.IDAL.IgprsNode CreategprsNode()
        {

            string ClassNamespace = AssemblyPath + ".gprsNode";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HYSoft.IDAL.IgprsNode)objType;
        }


        /// <summary>
        /// 创建gprsNodeLog数据层接口。
        /// </summary>
        public static HYSoft.IDAL.IgprsNodeLog CreategprsNodeLog()
        {

            string ClassNamespace = AssemblyPath + ".gprsNodeLog";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HYSoft.IDAL.IgprsNodeLog)objType;
        }


        /// <summary>
        /// 创建log数据层接口。
        /// </summary>
        public static HYSoft.IDAL.Ilog Createlog()
        {

            string ClassNamespace = AssemblyPath + ".log";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HYSoft.IDAL.Ilog)objType;
        }


        /// <summary>
        /// 创建m_coordinates数据层接口。
        /// </summary>
        public static HYSoft.IDAL.Im_coordinates Createm_coordinates()
        {

            string ClassNamespace = AssemblyPath + ".m_coordinates";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HYSoft.IDAL.Im_coordinates)objType;
        }


        /// <summary>
        /// 创建mac_value数据层接口。
        /// </summary>
        public static HYSoft.IDAL.Imac_value Createmac_value()
        {

            string ClassNamespace = AssemblyPath + ".mac_value";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HYSoft.IDAL.Imac_value)objType;
        }


        /// <summary>
        /// 创建manager数据层接口。
        /// </summary>
        public static HYSoft.IDAL.Imanager Createmanager()
        {

            string ClassNamespace = AssemblyPath + ".manager";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HYSoft.IDAL.Imanager)objType;
        }


        /// <summary>
        /// 创建monitor数据层接口。
        /// </summary>
        public static HYSoft.IDAL.Imonitor Createmonitor()
        {

            string ClassNamespace = AssemblyPath + ".monitor";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HYSoft.IDAL.Imonitor)objType;
        }


        /// <summary>
        /// 创建monitor_to_user数据层接口。
        /// </summary>
        public static HYSoft.IDAL.Imonitor_to_user Createmonitor_to_user()
        {

            string ClassNamespace = AssemblyPath + ".monitor_to_user";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HYSoft.IDAL.Imonitor_to_user)objType;
        }


        /// <summary>
        /// 创建node_to_monitor数据层接口。
        /// </summary>
        public static HYSoft.IDAL.Inode_to_monitor Createnode_to_monitor()
        {

            string ClassNamespace = AssemblyPath + ".node_to_monitor";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HYSoft.IDAL.Inode_to_monitor)objType;
        }


        /// <summary>
        /// 创建node_to_user数据层接口。
        /// </summary>
        public static HYSoft.IDAL.Inode_to_user Createnode_to_user()
        {

            string ClassNamespace = AssemblyPath + ".node_to_user";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HYSoft.IDAL.Inode_to_user)objType;
        }


        /// <summary>
        /// 创建sensor数据层接口。
        /// </summary>
        public static HYSoft.IDAL.Isensor Createsensor()
        {

            string ClassNamespace = AssemblyPath + ".sensor";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HYSoft.IDAL.Isensor)objType;
        }

        /// <summary>
        /// 创建sensor_node数据层接口。
        /// </summary>
        public static HYSoft.IDAL.Isensor_node Createsensor_node()
        {

            string ClassNamespace = AssemblyPath + ".sensor_node";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HYSoft.IDAL.Isensor_node)objType;
        }

        /// <summary>
        /// 创建bp_jz数据层接口。
        /// </summary>
        public static HYSoft.IDAL.Ibp_jz Createbp_jz()
        {

            string ClassNamespace = AssemblyPath + ".bp_jz";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HYSoft.IDAL.Ibp_jz)objType;
        }


        /// <summary>
        /// 创建bpcddata数据层接口。
        /// </summary>
        public static HYSoft.IDAL.Ibpcddata Createbpcddata()
        {

            string ClassNamespace = AssemblyPath + ".bpcddata";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HYSoft.IDAL.Ibpcddata)objType;
        }


        /// <summary>
        /// 创建bpinfo数据层接口。
        /// </summary>
        public static HYSoft.IDAL.Ibpinfo Createbpinfo()
        {

            string ClassNamespace = AssemblyPath + ".bpinfo";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HYSoft.IDAL.Ibpinfo)objType;
        }


        /// <summary>
        /// 创建bpjginfo数据层接口。
        /// </summary>
        public static HYSoft.IDAL.Ibpjginfo Createbpjginfo()
        {

            string ClassNamespace = AssemblyPath + ".bpjginfo";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HYSoft.IDAL.Ibpjginfo)objType;
        }


        /// <summary>
        /// 创建bppmzbinfo数据层接口。
        /// </summary>
        public static HYSoft.IDAL.Ibppmzbinfo Createbppmzbinfo()
        {

            string ClassNamespace = AssemblyPath + ".bppmzbinfo";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HYSoft.IDAL.Ibppmzbinfo)objType;
        }


        /// <summary>
        /// 创建config数据层接口。
        /// </summary>
        public static HYSoft.IDAL.Iconfig Createconfig()
        {

            string ClassNamespace = AssemblyPath + ".config";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HYSoft.IDAL.Iconfig)objType;
        }


        /// <summary>
        /// 创建result数据层接口。
        /// </summary>
        public static HYSoft.IDAL.Iresult Createresult()
        {

            string ClassNamespace = AssemblyPath + ".result";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HYSoft.IDAL.Iresult)objType;
        }


        /// <summary>
        /// 创建user数据层接口。
        /// </summary>
        public static HYSoft.IDAL.Iuser Createuser()
        {

            string ClassNamespace = AssemblyPath + ".user";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HYSoft.IDAL.Iuser)objType;
        }

        /// <summary>
        /// 创建monitor_info数据层接口。
        /// </summary>
        public static HYSoft.IDAL.Imonitor_info Createmonitor_info()
        {

            string ClassNamespace = AssemblyPath + ".monitor_info";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HYSoft.IDAL.Imonitor_info)objType;
        }

        /// <summary>
        /// 创建usr2monitor数据层接口。
        /// </summary>
        public static HYSoft.IDAL.Iusr2monitor Createusr2monitor()
        {

            string ClassNamespace = AssemblyPath + ".usr2monitor";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HYSoft.IDAL.Iusr2monitor)objType;
        }

        /// <summary>
        /// 创建usr2node数据层接口。
        /// </summary>
        public static HYSoft.IDAL.Iusr2node Createusr2node()
        {

            string ClassNamespace = AssemblyPath + ".usr2node";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HYSoft.IDAL.Iusr2node)objType;
        }

        /// <summary>
        /// 创建View_Log数据层接口。
        /// </summary>
        public static HYSoft.IDAL.IView_Log CreateView_Log()
        {

            string ClassNamespace = AssemblyPath + ".View_Log";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HYSoft.IDAL.IView_Log)objType;
        }
    }
}